package cn.broadmax.boot.modules.sys.controller;


import cn.broadmax.boot.base.SuperController;
import cn.broadmax.boot.base.message.SResult;
import cn.broadmax.boot.log.SysLog;
import cn.broadmax.boot.modules.sys.bean.param.SysParamSearch;
import cn.broadmax.boot.modules.sys.entity.SysParam;
import cn.broadmax.boot.modules.sys.mapper.SysAreaMapper;
import cn.broadmax.boot.modules.sys.service.ISysParamService;
import cn.broadmax.boot.util.SAssert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 系统参数 前端控制器
 * </p>
 *
 * @author jexx
 * @since 2021-07-02
 */
@Api(tags = "系统参数")
@RestController
@RequestMapping("/sys/param")
@RequiredArgsConstructor
public class SysParamController extends SuperController {

    @Autowired
    private final ISysParamService iSysParamService;

    @Autowired
    private final SysAreaMapper sysAreaMapper;

    @ApiOperation("分页查询系统参数")
    @GetMapping(value = "")
    public SResult<IPage<SysParam>> pageTypes(SysParamSearch search){
        LambdaQueryWrapper<SysParam> wrapper = SysParamSearch.createWrap(search);
        IPage<SysParam> page = iSysParamService.page(getPage(), wrapper);
        return SResult.ok("", page);
    }

    @ApiOperation("添加")
    @SysLog(value = "添加")
    @PutMapping(value = "/insert")
    public SResult<SysParam> InsertPage(@RequestBody SysParam param){
//        sysAreaMapper.insert(param);
        iSysParamService.save(param);
        param = iSysParamService.getById(param.getId());
        return SResult.ok("", param);
    }

    @ApiOperation("根据主键删除组织/部门")
    @SysLog(value = "删除组织/部门")
    @DeleteMapping(value = "/delete/{departmentId}")
    public SResult<Boolean> deleteDepartmentById(@PathVariable String departmentId) {
        boolean success = iSysParamService.removeById(departmentId);
        return SResult.ok("", success);
    }

    @ApiOperation("根据主键更新部门, 组织编码不能更新")
    @SysLog(value = "更新组织/部门")
    @PutMapping(value = "/update/{departmentId}")
    public SResult<SysParam> updateDepartmentById(@PathVariable String departmentId, @RequestBody SysParam department){
        System.out.println("-------111111");
        System.out.println(department.toString());
        SAssert.notNull(department);

        iSysParamService.updateById(department);

        department = iSysParamService.getById(department.getId());
        return SResult.ok("", department);
    }
}
